package com.aliyun.smartlight.mapper;


import com.aliyun.smartlight.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * Created by jackiechan on 2021/3/22 10:11
 *
 * @author jackiechan
 */
public interface UserMapper {

    @Insert("INSERT INTO users (username,password,type,status,data2,data1) VALUES (#{username},#{password},#{type},#{status},#{data2},#{data1})")
    int addUser(User user);

    @Select("SELECT  id, username,lastlogin,type,status  FROM users WHERE id =#{id}")
    User getUserById(Long id);

    @Select("SELECT  id, username,lastlogin,type,status  FROM users WHERE username =#{username}")
    User getUserByName(String username);


    @Select("SELECT  id, username,lastlogin,type,status  FROM users")
    List<User> getUsers();


    @Select("SELECT  id, username,lastlogin,type,status  FROM users WHERE type =#{type}")
    List<User> getUserByType(int type);

    @Update("UPDATE users set status =0 WHERE id = #{id}")
    int deleteUserById(Long id);

    @Update("<script>  update users\n" +
            "    <set>\n" +
            "\n" +
            "        <if test=\"password !=null and password !=''\">\n" +
            "            password = #{password},\n" +
            "        </if>\n" +
            "\n" +
            "        <if test=\"lastlogin !=null and lastlogin !=''\">\n" +
            "            password = #{password},\n" +
            "        </if>\n" +
            "\n" +
            "        <if test=\"lastlogin !=null and lastlogin !=''\">\n" +
            "            lastlogin = #{lastlogin},\n" +
            "        </if>\n" +
            "\n" +
            "        <if test=\"currentlogin !=null and currentlogin !=''\">\n" +
            "            currentlogin = #{currentlogin},\n" +
            "        </if>\n" +
            "        <if test=\"status !=null \">\n" +
            "            status = #{status},\n" +
            "        </if>\n" +
            "        <if test=\"type !=null \">\n" +
            "            type = #{type},\n" +
            "        </if>\n" +
            "        <if test=\"data2 !=null \">\n" +
            "            data2 = #{data2},\n" +
            "        </if>\n" +
            "        <if test=\"data1 !=null \">\n" +
            "            data1 = #{data1},\n" +
            "        </if>\n" +
            "\n" +
            "    </set>\n" +
            "\n" +
            "    WHERE id = #{id} </script>")
    int updateUser(User user);


    @Select("<script> select id, username,lastlogin,type,status from users\n" +
            "        <where>\n" +
            "            <if test=\"status ==0 || status ==1\">\n" +
            "             and    status =#{status}\n" +
            "            </if>\n" +
            "\n" +
            "            <if test=\"username !=null and username!=''\">\n" +
            "                and    username like concat('%',#{username},'%')\n" +
            "            </if>\n" +
            "        </where>\n</script>")
    List<User> getUsersByStatusAndUsername(@Param("status") int status,@Param("username") String username);

    @Update("<script> update users set status ='0'\n" +
            "        <where>\n" +
            "            id in\n" +
            "        <foreach collection=\"ids\" open=\"(\" close=\")\" item=\"id\" separator=\",\">\n" +
            "            #{id}\n" +
            "        </foreach>\n" +
            "\n" +
            "        </where></script>")
    int deleteByIds(@Param("ids") List<Long> ids);


}
